package com.example.apple.paopo.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.apple.paopo.R;
import com.example.apple.paopo.model.FoundModel;
import com.orhanobut.logger.Logger;

import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 17/12/25.
 */

public class mFoundAdapter extends RecyclerView.Adapter{

    private List<FoundModel> mDatas;
    private Context mContext;
    private LayoutInflater inflater;
    private ArrayList<String> mdatas;
    private mFoundImageAdapter mFoundImageAdapter;

    public mFoundAdapter(Context context, ArrayList mdatas) {
        this. mContext=context;
        this. mDatas=mdatas;
        inflater=LayoutInflater. from(mContext);
    }


    //初始化
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.mfoundadapter_item,parent, false);
        MyViewHolder holder= new MyViewHolder(view);
       // x.view().inject(holder,parent);
      //  View view= x.view().inject(this,inflater,container);
        return holder;
    }



    //返回一个ViewHolder,填充数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       MyViewHolder myViewHolder=(MyViewHolder)holder;
       myViewHolder.mfound_title.setText(mDatas.get(position).title);
       myViewHolder.mfound_likeNumber.setText(String.valueOf(mDatas.get(position).likeNumber));
       myViewHolder.mfound_replyNumber.setText(String.valueOf(mDatas.get(position).replyNumber));
       myViewHolder.mfound_originatorName.setText(mDatas.get(position).originatorName);
       ImageOptions imageOptions= new ImageOptions.Builder()
                .setFadeIn(true)
                .setCircular(true)
                .setCrop(true).setSize(20,20)
                .build(); //淡入效果
//ImageOptions.Builder()的一些其他属性：
//.setCircular(true) //设置图片显示为圆形
//                .setSquare(true) //设置图片显示为正方形
//                .setCrop(true).setSize(200,200) //设置大小
//                .setAnimation(animation) //设置动画
//                .setFailureDrawable(Drawable failureDrawable) //设置加载失败的动画
//                .setFailureDrawableId(int failureDrawable) //以资源id设置加载失败的动画
//.setLoadingDrawable(Drawable loadingDrawable) //设置加载中的动画
//                .setLoadingDrawableId(int loadingDrawable) //以资源id设置加载中的动画
//.setIgnoreGif(false) //忽略Gif图片
//                .setParamsBuilder(ParamsBuilder paramsBuilder) //在网络请求中添加一些参数
//                .setRaduis(int raduis) //设置拐角弧度
//.setUseMemCache(true) //设置使用MemCache，默认true
        x.image().bind(myViewHolder.mfound_originatorImage,mDatas.get(position).originatorImage , imageOptions);
        mdatas.clear();
        mdatas.addAll(mDatas.get(position).contentImage);


        mFoundImageAdapter.notifyDataSetChanged();
    }
    private void setUpRecyclerView(RecyclerView recyclerView) {
        mdatas= new ArrayList<>();
        mFoundImageAdapter= new mFoundImageAdapter(mContext,mdatas);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
//设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
//设置Adapter
        recyclerView.setAdapter(mFoundImageAdapter);
//        //设置分隔线
//        recyclerView.addItemDecoration(new DividerGridItemDecoration(getContext()));
//设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    //有多少个item
    @Override
    public int getItemCount() {
    //    Logger.d(String.valueOf(mDatas.size()));
        return mDatas.size();
    }


//    @ContentView(R.layout.mfoundadapter_item)
    //自定义MyViewHodel
    private class MyViewHolder extends RecyclerView.ViewHolder {

        TextView mfound_title;

        RecyclerView mfound_images;

        ImageView mfound_originatorImage;

        TextView mfound_originatorName;

        TextView mfound_likeNumber;

        TextView mfound_replyNumber;

        public MyViewHolder(View itemView) {
            super(itemView);
            mfound_title = (TextView) itemView.findViewById(R.id.mfound_title);
            mfound_images = (RecyclerView) itemView.findViewById(R.id.mfound_images);
            mfound_originatorImage = (ImageView) itemView.findViewById(R.id.mfound_originatorImage);
            mfound_originatorName = (TextView) itemView.findViewById(R.id.mfound_originatorName);
            mfound_likeNumber = (TextView) itemView.findViewById(R.id.mfound_likeNumber);
            mfound_replyNumber = (TextView) itemView.findViewById(R.id.mfound_replyNumber);
            setUpRecyclerView(mfound_images);
//            x.view().inject(this,R.layout.mfoundadapter_item,);
        }

//        public MyViewHolder(View view) {
//            super(view);
//            //  tv=(TextView) view.findViewById(R.id.tv_item);
//
//        }
    }
}
